package com.ltkj.order.controller;

import com.ltkj.common.core.domain.R;
import com.ltkj.common.core.utils.poi.ExcelUtil;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.order.domain.TicOrder;
import com.ltkj.order.domain.dto.*;
import com.ltkj.order.domain.vo.OrderVenuesDetailsVo;
import com.ltkj.order.domain.vo.TicOrderPageVo;
import com.ltkj.order.domain.vo.TicVenuesOrderPageVo;
import com.ltkj.order.domain.vo.UserCouponVo;
import com.ltkj.order.service.ITicOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2023-09-01
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/order")
public class TicOrderController extends BaseController
{
    @Autowired
    private ITicOrderService ticOrderService;



    /**
     * 导出订单列表
     */
    @ApiOperation("导出订单列表")
//    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TicOrder ticOrder)
    {
        List<TicOrder> list = ticOrderService.selectTicOrderList(ticOrder);
        ExcelUtil<TicOrder> util = new ExcelUtil<TicOrder>(TicOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
//    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TicOrder ticOrder)
    {
        return toAjax(ticOrderService.updateTicOrder(ticOrder));
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
//    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(ticOrderService.deleteTicOrderByOrderIds(orderIds));
    }

    /**
     * 新增订单
     */
    @ApiOperation("下订单——活动票类")
//    @RequiresPermissions("order:order:add")
    @Log(title = "下订单——活动票类", businessType = BusinessType.INSERT)
    @PostMapping("addTicOrder")
    public AjaxResult add(@RequestBody OrderAddDTO addDTO)
    {
        return ticOrderService.insertOrder(addDTO);
    }

    /**
     * 新增订单
     */
    @ApiOperation("下订单——其他类")
//    @RequiresPermissions("order:other:add")
    @Log(title = "下订单——其他类", businessType = BusinessType.INSERT)
    @PostMapping("addOtherOrder")
    public AjaxResult addOtherOrder(@RequestBody OrderAddDTO addDTO)
    {
        return ticOrderService.addOtherOrder(addDTO);
    }

    /**
     * 手动取消订单
     */
    @ApiOperation("取消订单")
//    @RequiresPermissions("order:order:cancle")
    @Log(title = "订单", businessType = BusinessType.CANCLE)
    @GetMapping("/cancle/{orderIds}")
    public AjaxResult cancle(@PathVariable Long[] orderIds)
    {
        return ticOrderService.cancleOrderByOrderIds(orderIds);
    }

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
//    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(TicOrderQueryDTO queryDTO)
    {
        startPage();
        List<TicOrderPageVo> list = ticOrderService.selectTicOrderListNew(queryDTO);
        return getDataTable(list);
    }

    /**
     * 活动抢票成功后获取订单Id
     */
    @ApiOperation("活动抢票成功后获取订单Id 参数为抢购成功后唯一标识")
//    @RequiresPermissions("order:orderrel:query")
    @GetMapping(value = "/orderResult/{key}")
    public AjaxResult getInfo(@PathVariable("key") String key)
    {
        return success(ticOrderService.selectOrderResult(key));
    }

    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取订单详细信息列表")
    @RequiresPermissions("order:detail:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(ticOrderService.selectTicOrderByOrderIdNew(orderId));
    }

    /**
     * 支付前获取订单详细
     */
    @ApiOperation("支付前获取订单详细")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/payOrder/{orderId}")
    public AjaxResult getPayInfo(@PathVariable("orderId") Long orderId)
    {
        return success(ticOrderService.selectTicOrderByOrderId(orderId));
    }
    /**
     * 支付前获取订单详细
     */
    @ApiOperation("微信支付前获取订单详细")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/wxPayOrder/{orderId}")
    public R<TicOrder> getWxPayInfo(@PathVariable("orderId") Long orderId)
    {
        return R.ok(ticOrderService.selectTicOrderByOrderId(orderId));
    }
    @ApiOperation("通过订单id获取订单详细")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/orderPayOrderIdDetail/{orderId}")
    public R<TicOrder> orderPayOrderIdDetail(@PathVariable("orderId") Long orderId)
    {
        return R.ok(ticOrderService.orderPayOrderIdDetail(orderId));
    }

    /**
     * 获取商家电话
     */
    @ApiOperation("订单获取商家电话")
//    @RequiresPermissions("order:order:paydetail")
    @PostMapping(value = "/serverPhone")
    public AjaxResult serverPhone(@RequestBody OrderPhoneQueryDTO queryDTO)
    {
        return success(ticOrderService.serverPhone(queryDTO));
    }

    /**
     * 获取商家电话
     */
    @ApiOperation("根据微信订单号查询订单信息")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/orderPayDetail/{wxOrdernum}")
    public R<TicOrder> orderPayDetail(@PathVariable("wxOrdernum") String wxOrdernum)
    {
        return R.ok(ticOrderService.orderPayDetail(wxOrdernum));
    }

    /**
     * 支付前获取订单详细
     */
    @ApiOperation("退款后退还库存及修改状态")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/orderBack/{orderId}")
    public AjaxResult orderBack(@PathVariable("orderId") Long orderId)
    {
        return ticOrderService.orderBack(orderId);
    }

    @ApiOperation("退款后退还库存及修改状态")
//    @RequiresPermissions("order:order:paydetail")
    @GetMapping(value = "/refundOrderStatus/{orderId}")
    public AjaxResult refundOrderStatus(@PathVariable("orderId") Long orderId)
    {
        return ticOrderService.refundOrderStatus(orderId);
    }

    /**
     * 获取当前用户的优惠券列表
     */
    @ApiOperation("小程序——获取当前用户的优惠券列表")
    @GetMapping(value = "/selectCouponList")
    public R<List<UserCouponVo>> selectCouponList()
    {
        return ticOrderService.selectCouponList();
    }

    /**
     * 获取当前用户购买票下可用的优惠券列表
     */
    @ApiOperation("小程序——获取当前用户购买票下可用的优惠券列表")
    @PostMapping(value = "/selectCouponListByTicket")
    public R<List<UserCouponVo>> selectCouponListByTicket(@RequestBody UserCouponQueryDTO queryDTO)
    {
        return ticOrderService.selectCouponListByTicket(queryDTO);
    }

    /**
     * 回填快递单号
     */
    @ApiOperation("回填快递单号")
    @GetMapping("/backfill")
    public AjaxResult backfill(Long orderDetailId,String trackingCode)
    {
        return ticOrderService.backfill(orderDetailId,trackingCode);
    }


    /**
     * 查询订单列表
     */
    @ApiOperation("查询场馆订单列表")
   @RequiresPermissions("order:order:list")
    @GetMapping("/venuesList")
    public TableDataInfo venuesList(TicVenuesOrderQueryDTO queryDTO)
    {
        startPage();
        List<TicVenuesOrderPageVo> list = ticOrderService.venuesList(queryDTO);
        return getDataTable(list);
    }
    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取场馆订单详细信息")
    @RequiresPermissions("order:detail:query")
    @GetMapping(value = "/venues/{orderId}")
    public R<OrderVenuesDetailsVo> getVenuesInfo(@PathVariable("orderId") Long orderId)
    {
        return R.ok(ticOrderService.getVenuesInfo(orderId));
    }

}
